﻿using BCCommon;
using BCDto.Common.TreeNode;
using BCDto.Sim.Admin.TreePermission;
using BCService.Sim.Admin.TreePermission;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace BCTMSRESTful.Controllers.Management.Admin.TreePermission
{
    /// <summary>
    /// 页面权限
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class TreePermissionConfigController : ManagementBaseController
    {
        private readonly ITreePermissionConfigService treePermissionConfigService;

        public TreePermissionConfigController(ITreePermissionConfigService treePermissionConfigService)
        {
            this.treePermissionConfigService = treePermissionConfigService;
        }

        /// <summary>
        /// 添加新版无限级树形权限
        /// </summary>
        /// <param name="requestDto">权限</param>
        /// <response code="201">添加新版无限级树形权限</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加新版无限级树形权限")]
        [ProducesResponseType(typeof(TreePermissionConfigDto), 201)]
        public IActionResult Post([FromBody] TreePermissionConfigRequestDto requestDto)
        {
            var result = this.treePermissionConfigService.Add(requestDto);
            return this.Ok(result);
        }

        /// <summary>
        /// 修改新版无限级树形权限
        /// </summary>
        /// <param name="treePermissionConfigId">权限Id</param>
        /// <param name="requestDto">权限</param>
        /// <response code="200">修改新版无限级树形权限</response>
        /// <returns></returns>
        [HttpPut("{tree_permission_config_id}")]
        [Log("修改新版无限级树形权限")]
        [ProducesResponseType(typeof(TreePermissionConfigDto), 200)]
        public IActionResult Put([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId, [FromBody] TreePermissionConfigRequestDto requestDto)
        {
            return Ok(this.treePermissionConfigService.Update(treePermissionConfigId, requestDto));
        }

        /// <summary>
        /// 修改新版无限级树形权限是否显示
        /// </summary>
        /// <param name="treePermissionConfigId">权限Id</param>
        /// <param name="isShow">是否显示</param>
        /// <response code="200">修改新版无限级树形权限</response>
        /// <returns></returns>
        [HttpPut("[action]/{tree_permission_config_id}/{is_show}")]
        [Log("修改新版无限级树形权限")]
        [ProducesResponseType(typeof(TreePermissionConfigDto), 200)]
        public IActionResult Show([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId, [FromRoute(Name = "is_show")] bool isShow)
        {
            this.treePermissionConfigService.UpdateShow(treePermissionConfigId, isShow);
            return NoContent();
        }
        

        /// <summary>
        /// 删除新版无限级树形权限
        /// </summary>
        /// <param name="treePermissionConfigId">权限Id</param>
        /// <returns></returns>
        /// <response code="204">删除新版无限级树形权限。</response>
        [HttpDelete("{tree_permission_config_id}")]
        [Log("删除新版无限级树形权限")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId)
        {
            this.treePermissionConfigService.Delete(treePermissionConfigId);
            return NoContent();
        }

        /// <summary>
        /// 获取树形新版无限级树形权限
        /// </summary>
        /// <response code="200">获取员工权限接口（分页）。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TreeNodeDto<TreePermissionConfigDto>>), 200)]
        public IActionResult Tree()
        {
            var list = this.treePermissionConfigService.GetTreePermission();
            return this.Ok(list);
        }

        /// <summary>
        /// 获取新版无限级树形权限
        /// </summary>
        /// <param name="permissionName">权限名称</param>
        /// <param name="permissionCode">权限码</param>
        /// <param name="permissionPathCode">权限路径码</param>
        /// <param name="parentPermissionPathCode">父级权限路径码</param>
        /// <param name="ascending">升序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">页面数据条数</param>
        /// <response code="200">获取员工权限接口（分页）。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TreePermissionConfigDto>), 200)]
        public IActionResult Query([FromQuery(Name = "permission_name")] string permissionName,
            [FromQuery(Name = "permission_code")] string permissionCode,
            [FromQuery(Name = "permission_path_code")] string permissionPathCode,
            [FromQuery(Name = "parent_permission_path_code")]string parentPermissionPathCode,
            [FromQuery(Name = "ascending")] bool? ascending = true,
            [FromQuery(Name = "index")] int pageIndex = 1,
            [FromQuery(Name = "size")] int pageSize = 10)
        {
            var list = this.treePermissionConfigService.Query(permissionName, permissionCode, permissionPathCode, parentPermissionPathCode, ascending, pageIndex, pageSize);
            return this.Ok(list);
        }

        /// <summary>
        /// 获取新版无限级树形权限
        /// </summary>
        /// <param name="permissionName">权限名称</param>
        /// <param name="permissionCode">权限码</param>
        /// <param name="permissionPathCode">权限路径码</param>
        /// <param name="parentPermissionPathCode">父级权限路径码</param>
        /// <response code="200">获取员工权限接口（分页）。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TreePermissionConfigDto>), 200)]
        public IActionResult List([FromQuery(Name = "permission_name")] string permissionName,
            [FromQuery(Name = "permission_code")] string permissionCode,
            [FromQuery(Name = "permission_path_code")] string permissionPathCode,
            [FromQuery(Name = "parent_permission_path_code")] string parentPermissionPathCode)
        {
            var list = this.treePermissionConfigService.GetList(permissionName, permissionCode, permissionPathCode, parentPermissionPathCode);
            return this.Ok(list);
        }
    }
}
